package com.ciaojian.client.util;

import org.mybatis.logging.Logger;
import org.mybatis.logging.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;

import java.util.Base64;

public class Base64StrToImage {

    Logger logger = LoggerFactory.getLogger(Base64StrToImage.class);

    public static MultipartFile base64MutipartFile(String imgStr) {
        try {
            Base64.Decoder decoder = Base64.getDecoder();
            String[] baseStr = imgStr.split(",");
            byte[] b = decoder.decode(baseStr[1]);
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {
                    b[i] += 256;
                }
            }
            return new BASE64DecodedMultipartFile(b, baseStr[0]);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}